package cn.edu.sgu.www.mhxysy.service.repository.impl;

import cn.edu.sgu.www.mhxysy.entity.prop.Prop;
import cn.edu.sgu.www.mhxysy.entity.repository.PackageGrid;
import cn.edu.sgu.www.mhxysy.entity.repository.PackageProp;
import cn.edu.sgu.www.mhxysy.entity.repository.RolePackage;
import cn.edu.sgu.www.mhxysy.mapper.prop.PropMapper;
import cn.edu.sgu.www.mhxysy.mapper.repository.PackageGridMapper;
import cn.edu.sgu.www.mhxysy.mapper.repository.PackagePropMapper;
import cn.edu.sgu.www.mhxysy.mapper.repository.RolePackageMapper;
import cn.edu.sgu.www.mhxysy.service.repository.PackagePropService;
import cn.edu.sgu.www.mhxysy.vo.prop.PropVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class PackagePropServiceImpl implements PackagePropService {

    private final PropMapper propMapper;
    private final PackageGridMapper packageGridMapper;
    private final PackagePropMapper packagePropMapper;
    private final RolePackageMapper rolePackageMapper;

    @Autowired
    public PackagePropServiceImpl(
            PropMapper propMapper,
            PackageGridMapper packageGridMapper,
            PackagePropMapper packagePropMapper,
            RolePackageMapper rolePackageMapper) {
        this.propMapper = propMapper;
        this.packageGridMapper = packageGridMapper;
        this.packagePropMapper = packagePropMapper;
        this.rolePackageMapper = rolePackageMapper;
    }

    @Override
    public void arrange(String roleId) {
        QueryWrapper<RolePackage> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("role_id", roleId);

        RolePackage rolePackage = rolePackageMapper.selectOne(queryWrapper);

        if (rolePackage != null) {
            // 查询当前包裹的所有格子
            QueryWrapper<PackageGrid> queryWrapper1 = new QueryWrapper<>();

            queryWrapper1.select("id");
            queryWrapper1.orderByAsc("location");
            queryWrapper1.eq("package_id", rolePackage.getId());

            List<PackageGrid> packageGrids = packageGridMapper.selectList(queryWrapper1);

            // 得到格子ID列表
            List<String> gridIds = packageGrids.stream().map(new Function<PackageGrid, String>() {
                @Override
                public String apply(PackageGrid packageGrid) {
                    return packageGrid.getId();
                }
            }).collect(Collectors.toList());

            // 查询当前包裹的所有道具
            QueryWrapper<PackageProp> queryWrapper2 = new QueryWrapper<>();

            queryWrapper2.in("grid_id", gridIds);

            List<PackageProp> packageProps = packagePropMapper.selectList(queryWrapper2);

            // 包裹没有装满
            gridIds = packageProps.stream().map(new Function<PackageProp, String>() {
                @Override
                public String apply(PackageProp packageProp) {
                    return packageProp.getGridId();
                }
            }).collect(Collectors.toList());

            if (gridIds.size() < packageGrids.size()) {
                // 整理格子，让位置自动调整
                AtomicInteger location = new AtomicInteger();

                for (PackageGrid packageGrid : packageGrids) {
                    String gridId = packageGrid.getId();

                    if (gridIds.contains(gridId)) {
                        location.addAndGet(1);

                        // 查询格子放置的道具
                        QueryWrapper<PackageProp> queryWrapper3 = new QueryWrapper<>();

                        queryWrapper3.eq("grid_id", gridId);

                        PackageProp packageProp = packagePropMapper.selectOne(queryWrapper3);

                        PackageGrid grid = packageGrids.get(location.get() - 1);

                        // 1、修改道具所在的格子ID
                        packageProp.setGridId(grid.getId());

                        packagePropMapper.updateById(packageProp);

                        // 2、修改道具位置
                        UpdateWrapper<Prop> updateWrapper = new UpdateWrapper<>();

                        updateWrapper.eq("id", packageProp.getPropId());
                        updateWrapper.set("location", location.get());

                        propMapper.update(updateWrapper.getEntity(), updateWrapper);
                    }
                }
            }
        }
    }

    @Override
    public List<PropVO> selectByRoleId(String roleId) {
        return packagePropMapper.selectByRoleId(roleId);
    }

}